import { getToken } from '@/utils/storage'
import { Message } from 'element-ui'
import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    // {path:'/' , redirect:'dashboard'},
    {
      path: '/',
      // 默认会首先找 index.vue
      component: () => import('@/views/layout'),
      redirect: 'dashboard',
      // 二级路由的 path 有两种写法:
      // 1. 绝对路径
      // /dashboard
      // 2. 相对路径 自动拼接上父路由地址 => /dashboard
      // /home/dashboard
      children: [
        { path: '/dashboard', component: () => import('@/views/dashboard') },
        { path: '/article', component: () => import('@/views/article') }
      ]
    },
    { path: '/login', component: () => import('@/views/login') }
  ]
})

// 导航守卫
const whiteList = ['/login']
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (token || whiteList.includes(to.path)) return next()
  next('/login')
  Message.error('请您先登录哦~')
})

export default router
